package com.nsgf.library.ui.adapter;

import android.content.Context;
import android.view.ContextThemeWrapper;
import android.widget.CheckBox;

import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.nsgf.library.R;
import com.nsgf.library.data.bean.StructureComponentBean;
import com.nsgf.library.databinding.ItemStructureComponentBinding;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;

/**
 * 结构组件检查项适配器
 */
public class StructureComponentAdapter extends SimpleDataBindingAdapter<StructureComponentBean, ItemStructureComponentBinding> {

    public StructureComponentAdapter(Context context) {
        super(context, R.layout.item_structure_component, new DiffUtil.ItemCallback<StructureComponentBean>() {
            @Override
            public boolean areItemsTheSame(StructureComponentBean oldItem, StructureComponentBean newItem) {
                return oldItem.getComponentName().equals(newItem.getComponentName());
            }

            @Override
            public boolean areContentsTheSame(StructureComponentBean oldItem, StructureComponentBean newItem) {
                return oldItem.getComponentName().equals(newItem.getComponentName()) &&
                       oldItem.getCheckItems().equals(newItem.getCheckItems());
            }
        });
    }

    @Override
    protected void onBindItem(ItemStructureComponentBinding binding, StructureComponentBean item, RecyclerView.ViewHolder holder) {
        binding.setComponent(item);
        
        // 动态创建检查项CheckBox
        binding.llCheckItems.removeAllViews();
        
        // 使用嵌套LinearLayout实现每行两个
        android.widget.LinearLayout currentRow = null;
        for (int i = 0; i < item.getCheckItems().size(); i++) {
            StructureComponentBean.CheckItem checkItem = item.getCheckItems().get(i);
            
            // 每两个创建一个新行
            if (i % 2 == 0) {
                currentRow = new android.widget.LinearLayout(binding.getRoot().getContext());
                currentRow.setOrientation(android.widget.LinearLayout.HORIZONTAL);
                android.widget.LinearLayout.LayoutParams rowParams = new android.widget.LinearLayout.LayoutParams(
                    android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
                    android.widget.LinearLayout.LayoutParams.WRAP_CONTENT
                );
                currentRow.setLayoutParams(rowParams);
                binding.llCheckItems.addView(currentRow);
            }
            
            // 使用样式创建CheckBox
            Context context = binding.getRoot().getContext();
            ContextThemeWrapper wrapper = new ContextThemeWrapper(context, R.style.PatrolCheckboxStyle);
            CheckBox checkBox = new CheckBox(wrapper, null, 0);
            checkBox.setText(checkItem.getName());
            checkBox.setChecked(checkItem.isSelected());
            checkBox.setTextSize(12);
            checkBox.setTextColor(binding.getRoot().getContext().getResources().getColor(R.color.color_495662));
            
            // 设置点击监听
            final int itemIndex = i;
            checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                checkItem.setSelected(isChecked);
            });
            
            // 设置布局参数（每行两个，每个占一半）
            android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
                0,
                android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
                1.0f
            );
            // 设置左右间距
            int margin = 10; // 间距大小，单位dp，需要转换为px
            int marginPx = (int) (margin * binding.getRoot().getContext().getResources().getDisplayMetrics().density);
            params.setMargins(marginPx, marginPx, marginPx, marginPx);
            checkBox.setLayoutParams(params);
            
            if (currentRow != null) {
                currentRow.addView(checkBox);
            }
        }
        
        binding.executePendingBindings();
    }
}

